package com.education.admin.api.shiro;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro bean 实例配置
 * @author Jason
 * @version 1.0.0
 * @date 2021-11-21 00:52
 */
@Configuration
@AutoConfigureAfter(ShiroLifecycleBeanPostProcessorConfig.class)
public class ShiroBeanConfig {

    private static final long INVALID_TIME = 3600 * 6 * 1000;

    /**
     * @param securityManager 参数名必须与下面的方法名一致
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 配置URL的拦截方式
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        //filterChainDefinitionMap.put("/test", "anon");
        //filterChainDefinitionMap.put("/getUser", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        shiroFilterFactoryBean.setFilters(shiroFilterFactoryBean.getFilters());
        return shiroFilterFactoryBean;
    }

    /**
     * @param systemRealm 参数名要与同路径下的SystemRealm类名一致，
     *                    并且SystemRealm类需要加@Component注解
     * @param ehCacheManager 参数名必须与下面的方法名一致
     * @return
     */
    @Bean
    public SecurityManager securityManager(Realm systemRealm,
                                           CacheManager ehCacheManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(systemRealm);
        // 设置缓存管理器
        securityManager.setCacheManager(ehCacheManager);
        return securityManager;
    }

    @Bean
    public SessionManager sessionManager() {
        DefaultSessionManager sessionManager = new DefaultSessionManager();
        // 设置session有效期为6小时
        sessionManager.setGlobalSessionTimeout(INVALID_TIME);
        return sessionManager;
    }

    /**
     * 初始化缓存管理器
     * @param cacheManager net.sf.ehcache.CacheManager
     * @return org.apache.shiro.cache.CacheManager
     */
    @Bean
    public CacheManager ehCacheManager(net.sf.ehcache.CacheManager cacheManager) {
        EhCacheManager ehCacheManager = new EhCacheManager();
        ehCacheManager.setCacheManager(cacheManager);
        return ehCacheManager;
    }

    @Bean
    public net.sf.ehcache.CacheManager cacheManager() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
        return net.sf.ehcache.CacheManager.create(inputStream);
    }

    /**
     * 开启AOP权限注解
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        // 开启AOP权限注解
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

}
